package com.example.myinput;

import android.graphics.PointF;
import java.util.List;
import java.util.Random;
import java.util.ArrayList;

public class BezierUtil {
    private static final Random random = new Random();

    public static List<PointF> bezierCreate(float x1, float y1, float x2, float y2,
                                            float x3, float y3, float x4, float y4,
                                            int numberOfPoints, Float mh) {
        float h = randomNum(0, 300);
        if (mh != null) {
            h = mh;
        }

        PointF[] cp = new PointF[]{
                new PointF(x1, y1 + h),
                new PointF(x2, y2 + h),
                new PointF(x3, y3 + h),
                new PointF(x4, y4 + h),
        };

        List<PointF> curve = new ArrayList<>();
        float dt = 1.0f / (numberOfPoints - 1);

        for (int i = 0; i < numberOfPoints; i++) {
            float t = dt * i;
            float tSquared = t * t;
            float tCubed = tSquared * t;

            float cx = 3.0f * (cp[1].x - cp[0].x);
            float bx = 3.0f * (cp[2].x - cp[1].x) - cx;
            float ax = cp[3].x - cp[0].x - cx - bx;

            float cy = 3.0f * (cp[1].y - cp[0].y);
            float by = 3.0f * (cp[2].y - cp[1].y) - cy;
            float ay = cp[3].y - cp[0].y - cy - by;

            float resultX = ax * tCubed + bx * tSquared + cx * t + cp[0].x;
            float resultY = ay * tCubed + by * tSquared + cy * t + cp[0].y;

            curve.add(new PointF(resultX, resultY));
        }
        List<PointF> array = new ArrayList<>();
        for (int i = 0; i < curve.size(); i++) {
            try {
                int j = i < numberOfPoints ? i : 199 - i;
                float xx = curve.get(j).x;
                float yy = Math.abs(h - curve.get(j).y);
                array.add(new PointF(xx, yy));
            } catch (Exception e) {
                break;
            }
        }
        return array;
    }

    public static List<PointF> randomPoints(float sx, float sy, float ex, float ey,
                                            int nums, Float mLeaveHeightLength, Float mh) {
        float leaveHeightLength = randomNum(0, 1000);
        if (mLeaveHeightLength != null) {
            leaveHeightLength = mLeaveHeightLength;
        }



        float x2, y2, x3, y3;
        if (Math.abs(ex - sx) > Math.abs(ey - sy)) {
            float my = (sy + ey) / 2;
            y2 = my + randomNum(0, leaveHeightLength);
            y3 = my - randomNum(0, leaveHeightLength);
            if (y3 < 0) y3 = 0;

            float msx = Math.min(sx, ex);
            float lx = Math.abs(sx - ex) / 3;
            x2 = msx + lx / 2 + randomNum(0, lx);
            x3 = msx + lx + lx / 2 + randomNum(0, lx);
        } else {
            float mx = (sx + ex) / 2;
            x2 = mx + randomNum(0, leaveHeightLength);
            x3 = mx - randomNum(0, leaveHeightLength);
            if (x3 < 0) x3 = 0;

            float msy = Math.min(sy, ey);
            float ly = Math.abs(sy - ey) / 3;
            y2 = msy + ly / 2 + randomNum(0, ly);
            y3 = msy + ly + ly / 2 + randomNum(0, ly);
        }

        return bezierCreate(sx, sy, x2, y2, x3, y3, ex, ey, nums, mh);
    }

    private static float randomNum(float min, float max) {
        return min + random.nextFloat() * (max - min);
    }
}
